package com.atguigu.springbootdemo.controller;

import com.atguigu.springbootdemo.bean.Employee;
import com.atguigu.springbootdemo.bean.Region;
import com.atguigu.springbootdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Smexy on 2023/1/7
 */
@RestController
public class EmployeeController
{
    //employeeService = new EmployeeService()
    // new EmployeeService()； 改为从容器中获取，通知容器，给我准备一个EmployeeService()类型的对象
    // 自动根据当前注解标注的属性的类型，从容器中找匹配的，找到就赋值
    // Autowired  等价于 =
    @Autowired
    //多态  变量使用的父类的类型，实际赋值使用子类类型。 优点： service层变化，无需修改控制器。分层解耦
    private EmployeeService employeeService;
    //private EmployeeServiceImpl2 employeeService;

    @RequestMapping(value = "/emp")
    public Object handle1(String op,Integer id,String lastname,String gender,String email){

        //把收到的参数封装为数据模型
        Employee e = new Employee(id, lastname, email, gender);

        switch (op){
            case "select": if (id == null){
                return "id非法";
            }else {
                //要查询的那个人不存在
                Employee employee = employeeService.getEmployeeById(id);
                return employee == null ? "查无此人" : employee;
            }
            case "delete": if (id == null){
                return "id非法";
            }else {
                employeeService.deleteEmployeeById(id);
                return "ok";
            }
            case "insert": employeeService.insertEmployee(e); return "ok";

            case "update": if (id == null){
                return "id非法";
            }else {
                employeeService.updateEmployee(e);
                return "ok";
            }
            default:  return "ok";
        }

    }

    @RequestMapping(value = "/getAllEmp")
    public Object handle2(){
        List<Employee> all = employeeService.getAll();
        return all;
    }

    @RequestMapping(value = "/getAllRegion")
    public Object handle3(){
        List<Region> allRegion = employeeService.getAllRegion();
        return allRegion;
    }


}
